clear
set more off

global lecmen    "\\intra\partages\au_amic2\SRCV\TABLES_INITIALES\INPUT\MENAGES"
global lecmensas "\\intra\partages\au_amic2\SRCV_AVRIL2021\TABLES_INITIALES_AVRIL2021\MENAGES"
global ecrmen    "\\intra\partages\au_amic2\SRCV_AVRIL2021\BASES_FEV2022_V5\MENAGES"


/* 1 - EXTRACTION DES VARIABLES SELECTIONNéES */
/* a - fichier STATA 1ère livraison 2008-2017 */
foreach an of numlist 8  9 {
cd "$lecmen"
use menages0`an'_diff.dta, clear
keep 	men pond_* idmenc_* ///
		hy090n hy040n hy100n hy050n hy070n hy060n hy080n hy130n hy110n hy145n hy120n hy020 db030
gen annee_SRCV = 200`an'
order men annee_SRCV 
cd "$ecrmen"
save men_`an'.dta, replace
display `an'
count 
}

foreach an of numlist 10 / 12 {
cd "$lecmen"
use menages`an'_diff.dta, clear
keep 	men pond_* idmenc_* ///
		hy090n hy040n hy100n hy050n hy070n hy060n hy080n hy130n hy110n hy145n hy120n hy020 db030
gen annee_SRCV = 20`an'
order men annee_SRCV 
cd "$ecrmen"
save men_`an'.dta, replace
display `an'
count 
}

foreach an of numlist 13/17 {
cd "$lecmen"
use menages`an'_diff.dta, clear
keep 	men pond_* idmenc_* ///
		hy090n hy040n hy100n hy050n hy070n hy060n hy080n hy130n hy110n hy145n hy120n hy020 db030
gen annee_SRCV = 20`an'
order men annee_SRCV 
cd "$ecrmen"
save men_`an'.dta, replace
display `an'
count 
}

/* b - fichier SAS 2nde livraison 2018-2019 */
foreach an of numlist 18 / 19 {
cd "$lecmensas"
use menages_20`an'.dta, clear
keep 	men pond_* IDMENC_* ///
		HY090N HY040N HY100N HY050N HY070N HY060N HY080N HY130N HY110N HY145N HY120N HY020 DB030
gen annee_SRCV = 20`an'
order men annee_SRCV 
cd "$ecrmen"
save men_`an'.dta, replace
display `an'
count 
}


/* 2 - CHANGEMENT DE NOM DES VARIABLES */
foreach an of numlist 8 / 17 {
cd "$ecrmen"
use men_`an'.dta, clear
rename hy090n HY090N
rename hy040n HY040N
rename hy100n HY100N
rename hy050n HY050N
rename hy070n HY070N
rename hy060n HY060N
rename hy080n HY080N
rename hy130n HY130N
rename hy110n HY110N
rename hy145n HY145N
rename hy120n HY120N
rename hy020  HY020
rename idmenc_* idmenc
rename pond_* pond_men
save men_`an'.dta, replace
}

foreach an of numlist 18 / 19 {
cd "$ecrmen"
use men_`an'.dta, clear
rename IDMENC_* idmenc
rename pond_* pond_men
rename DB030 db030
save men_`an'.dta, replace
}


/* 3 - EMPILEMENT DES TABLES */
clear 
use "$ecrmen\men_8.dta", clear
append using 	"$ecrmen\men_9.dta" "$ecrmen\men_10.dta" "$ecrmen\men_11.dta" ///
				"$ecrmen\men_12.dta" "$ecrmen\men_13.dta" "$ecrmen\men_14.dta" ///
				"$ecrmen\men_15.dta" "$ecrmen\men_16.dta" "$ecrmen\men_17.dta" ///
				"$ecrmen\men_18.dta" "$ecrmen\men_19.dta" 
count /* 134 473 + 17 var */
order idmenc annee_SRCV men
sort idmenc annee_SRCV men				
save "$ecrmen\menages_2008_2019.dta", replace


/* 4 - LABELS */
clear 
use "$ecrmen\menages_2008_2019.dta", clear
label variable men 			"Identifiant longitudinal du menage" 
label variable annee_SRCV	"annee de l'enquete/du fichier"
label variable pond_men		"Ponderation transversale menages"
label variable HY040N		"Revenu net tire de la location de propriete ou terres"
label variable HY050N		"Montant net redresse des alloc. famille/enfants"
label variable HY060N		"Montant net des alloc. d'exclusion sociale non classées ailleurs"
label variable HY070N		"Montant net redresse des aides au logement"
label variable HY080N		"Transferts nets percus entre menages (loyers, pensions)"
label variable HY090N		"Montant annuel net interets, div., benef. tires d'invest. en capital"
label variable HY100N		"Montant annuel net des remboursements interets hypothecaires"
label variable HY110N		"Revenus nets percus par personnes moins 16 ans"
label variable HY120N		"Montant net impot fortune"
label variable HY130N		"Montant net transfert inter-menages verses regulierement en especes"
label variable HY145N		"Rembours./encaissements lies a ajustement d'impot"
label variable HY020		"Revenu total disponible du menage"
label variable db030		"Identifiant européen du ménage"
save "$ecrmen\menages_2008_2019.dta", replace
clear 
use "$ecrmen\menages_2008_2019.dta", clear
